import numpy as N
from numpy.ctypeslib import load_library
from numpyctypes import c_ndarray
import ctypes as C

mylib = load_library('libMRF', '../cpp/')       # '.' is the directory of the C++ lib  

def MRF(array1, array2):
    arg1 = c_ndarray(array1, dtype=N.double, ndim = 3)
    arg2 = c_ndarray(array2, dtype=N.double, ndim = 3)
    return mylib.MRF(arg1, arg2)
"""
def quickGraphCut(n_seg, image, labelArray, miuArray, covarMatArray,lnCovarMatDet ):
    arg1 = C.c_int(n_seg)
    arg2 = c_ndarray(image,         dtype = N.double, ndim = 2)
    arg3 = c_ndarray(labelArray,    dtype = N.double, ndim = 2)
    arg4 = c_ndarray(miuArray,      dtype = N.double, ndim = 2)
    arg5 = c_ndarray(covarMatArray, dtype = N.double, ndim = 3)
    arg6 = c_ndarray(lnCovarMatDet, dtype = N.double, ndim = 1)
    
    return mylib.quickGraphCut(arg1,arg2,arg3,arg4,arg5,arg6)	
""" 


def quickGraphCut(n_seg, R, G, B, L, labelArray, miuArray, covarMatArray,lnCovarMatDet ):
    arg1 = C.c_int(n_seg)
    arg2 = c_ndarray(R,         dtype = N.double, ndim = 2)
    arg3 = c_ndarray(G,         dtype = N.double, ndim = 2)
    arg4 = c_ndarray(B,         dtype = N.double, ndim = 2)
    arg5 = c_ndarray(L,         dtype = N.double, ndim = 2)
    arg6 = c_ndarray(labelArray,    dtype = N.double, ndim = 2)
    arg7 = c_ndarray(miuArray,      dtype = N.double, ndim = 2)
    arg8 = c_ndarray(covarMatArray, dtype = N.double, ndim = 3)
    arg9 = c_ndarray(lnCovarMatDet, dtype = N.double, ndim = 1)
    
    return mylib.quickGraphCut(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)	

def alphaExpansionQuickGraphCut(R, G, B, labelArray, miuArray, covarMatArray,lnCovarMatDet, activeContours ):

    arg1 = c_ndarray(R,         dtype = N.double, ndim = 2)
    arg2 = c_ndarray(G,         dtype = N.double, ndim = 2)
    arg3 = c_ndarray(B,         dtype = N.double, ndim = 2)
    arg4 = c_ndarray(labelArray,    dtype = N.double, ndim = 2)
    arg5 = c_ndarray(miuArray,      dtype = N.double, ndim = 2)
    arg6 = c_ndarray(covarMatArray, dtype = N.double, ndim = 3)
    arg7 = c_ndarray(lnCovarMatDet, dtype = N.double, ndim = 1)
    arg8 = c_ndarray(activeContours, dtype = N.double, ndim = 2)
    
    return mylib.alphaExpansionQuickGraphCut(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)	


def EMProcess_Test( R, G, B ):
    
    arg1 = c_ndarray(R,dtype = N.double, ndim = 2)
    arg2 = c_ndarray(G,dtype = N.double, ndim = 2) 
    arg3 = c_ndarray(B,dtype = N.double, ndim = 2)

    
    return mylib.EMProcess_Test(arg1,arg2,arg3)	


def EMProcess(pi_m, new_pi_m, R, G, B, miuArray, new_miuArray, inversedCovarianceMatrixArray, new_covarMatArray, covarMatDet):
    
    arg1 = c_ndarray(pi_m,             dtype = N.double, ndim = 1)
    arg2 = c_ndarray(new_pi_m,         dtype = N.double, ndim = 1)
    
    arg3 = c_ndarray(R,         dtype = N.double, ndim = 2)
    arg4 = c_ndarray(G,         dtype = N.double, ndim = 2)
    arg5 = c_ndarray(B,         dtype = N.double, ndim = 2)
    
    arg6 = c_ndarray(miuArray,                          dtype = N.double, ndim = 2)
    arg7 = c_ndarray(new_miuArray,                      dtype = N.double, ndim = 2)
    arg8 = c_ndarray(inversedCovarianceMatrixArray,     dtype = N.double, ndim = 3)
    arg9 = c_ndarray(new_covarMatArray,                 dtype = N.double, ndim = 3)
    arg10 = c_ndarray(covarMatDet,                      dtype = N.double, ndim = 1)
        
    mylib.EMProcess(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)	